package twitter.api;

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;

class MahTwitter
{
	private var twitterResponse:String;
	
	public function new(){
		// kveristring
		var queryString = "testdrive";
		// pot do php scripte
		var twitterPHPScriptPath:String = "http://localhost/tweets2bits/test.php?q=";
		// pozenem src!
		searchTwitter(twitterPHPScriptPath + queryString);
	}

	private function searchTwitter(URL:String)
	{
		var urlLoader:URLLoader = new URLLoader();
		// ko se nalozi ves junk, prozim finishSearching
		urlLoader.addEventListener(Event.COMPLETE, finishSearching);
		urlLoader.load(new URLRequest(URL));			
	}

	private function finishSearching(e:Event = null)
	{
		// Vs krep je prisel iz php-ja sem, woohooo!!!
		// popucej mal za sabo...
		e.target.removeEventListener(Event.COMPLETE, finishSearching);

		// nafilej respons s podatki, ki jih je potegnu s strani
		twitterResponse = new String(e.target.data);
		showResults();
	}

	private function showResults()
	{
		// rezultat je xml, tako da sparsam
		var xml = Xml.parse(twitterResponse);
		var fast = new haxe.xml.Fast(xml.firstElement());
		var entries = fast.nodes.entry;
		
		// feed > entry > content
		// iterate over numbers
		for(entry in entries) {
			trace(entry.node.content.innerData);
		}
	}
}